<script setup>
import {ref } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import {http} from "@/api";
import {ElMessage} from "element-plus";
const router = useRouter();
const store = useStore();
const showType = ref('1')
const checkCode = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('请输入验证码'))
  }
  if (value.toLowerCase() !== code.value.toLowerCase()) {
    return callback(new Error('验证码错误'))
  }
  callback()
}
const rules = ref({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  nickname: [
    { required: true, message: '请输入昵称', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  passwordConfirm: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        if (value !== loginForm.value.password) {
          return callback(new Error('两次输入密码不一致'))
        }
        callback()
      }, trigger: 'blur' }
  ]
})
const loginForm = ref({
  nickname: '',
  username: '',
  password: '',
  passwordConfirm: ''
})
const loginFormRef = ref(null)
function toRegister() {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      ElMessage.success('注册成功')
      await http.user.create({
        username: loginForm.value.username,
        password: loginForm.value.password,
        nickname: loginForm.value.nickname,
        type: 0
      })
      await router.replace({
        path: '/login'
      })
    } else {
      return false
    }
  })
}
const toLogin = () => {
  router.push({
    path: '/login'
  })
}
</script>


<template>
  <div class="login">
    <div class="login-form f-c a-c j-c">
      <div class="login-title">律师资讯平台</div>

      <div class="register-form-box f-c a-c j-c">
        <div class="name">注册</div>
        <el-form ref="loginFormRef" label-position="top" size="large" :model="loginForm" :rules="rules">
          <el-form-item label="" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入账号">
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="nickname">
            <el-input v-model="loginForm.nickname" placeholder="请输入昵称">
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="请输入密码">
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="passwordConfirm">
            <el-input type="password"  v-model="loginForm.passwordConfirm" placeholder="请确认密码">
            </el-input>
          </el-form-item>
        </el-form>
        <el-button size="large" style="width: 100%" type="primary" @click="toRegister">注册</el-button>
        <div style="width: 100%;text-align: right">
          <el-button type="text" @click="toLogin">立即登录</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background-image: url("~@/assets/login_bg.jpeg");
  background-size: 100% 100%;
  background-position: center center;
  background-repeat: no-repeat;
  overflow: auto;
}
.login-form {
  .login-title {
    font-family: 优设标题黑;
    font-size: 52px;
    font-weight: normal;
    line-height: 72px;
    text-align: center;
    letter-spacing: 0em;
    color: #FFFFFF;
    width: 575px;
    height: 43px;
    margin: 100px 0;
  }
}
.register-form-box{
  width: 513px;
  /* 矩形 217 */
  border-radius: 8px;
  background: #FFFFFF;
  padding: 29px 38px;
  .name  {
    font-size: 24px;
    color: #333333;
    margin-bottom: 27px;
    text-align: left;
    width: 100%;
    font-weight: bold;
  }
  .el-form {
    width: 100%;
  }
}
</style>